boxyTheme = {
  light: {
    toolbox: {
      bar: { color: "#525252", background: "#ffffff" },
      flyout: { background: "#ffffff" },
      label: {
        selected: { color: "#000000", background: "#ffffff" },
        unselected: { color: "#000000", background: "transparent" },
      },
    },
    workspace: { background: "#ffffff" },
  },
  dark: {
    toolbox: { bar: { color: "#ffffff", background: "#373737" }, flyout: { background: "#202020" } },
    workspace: { background: "#2e2e2e" },
  },
};

function getCssVariables(theme, father = "") {
  let variables = {};
  for (let key in theme) {
    if (typeof theme[key] === "object") {
      variables = Object.assign(variables, getCssVariables(theme[key], father + "-" + key));
    } else {
      variables[father + "-" + key] = theme[key];
    }
  }
  return variables;
}

function setCssVariables(theme) {
  let cssVariables = getCssVariables(theme);
  for (let key in cssVariables) {
    document.documentElement.style.setProperty("-" + key, cssVariables[key]);
  }
}

function getCookie(name) {
  let strCookie = document.cookie;
  let arrCookie = strCookie.split("; ");
  for (let i = 0; i < arrCookie.length; i++) {
    let arr = arrCookie[i].split("=");
    if (arr[0] === name) {
      return arr[1];
    }
  }
  return "";
}

setCssVariables(boxyTheme[getCookie("themeMode") || "light"]);
